using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using AUSRIS.Reports.Utilities.Deployment.Entities;
using AUSRIS.Reports.Utilities.Deployment.Logging;

namespace AUSRIS.Reports.Utilities.Deployment.FileSystem
{
    /// <summary>
    /// Base class for IRepository implementations
    /// </summary>
    public abstract class AbstractRepository : IRepository
    {
        #region Private Fields

        private ILogger logger;

        #endregion

        #region Dependencies

        public ILogger Logger
        {
            get
            {
                return logger;
            }
            set
            {
                logger = value;
            }
        }

        #endregion

        #region IRepository Members

        public abstract void Checkout(CatalogFile file, string versionTag);

        public abstract void SetRevision(CatalogFile file);

        public abstract void CheckoutModule(string moduleName, string versionTag);

        public virtual void SetDataSourceViewPath(Model model)
        {
            model.DataSourceViewPath = Path.GetFullPath(Path.Combine(this.GetWorkingDirectory(), model.DataSourceViewPath));
        }

        public virtual bool FileExists(CatalogFile file)
        {
            return File.Exists(this.GetLocalPath(file));
        }

        #endregion

        #region Protected Methods

        protected abstract string GetWorkingDirectory();

        protected virtual void LogMessage(string format, params object[] args)
        {
            if (this.Logger != null)
            {
                this.Logger.LogMessage(" " + format, args);
            }
        }

        protected virtual string GetLocalPath(CatalogFile file)
        {
            return Path.GetFullPath(Path.Combine(this.GetWorkingDirectory(), file.SourcePath));
        }

        protected virtual void ClearWorkingFolder()
        {
            string workingDirectory = this.GetWorkingDirectory();
            if (Directory.Exists(workingDirectory))
            {
                Directory.Delete(workingDirectory, true);
            }
            Directory.CreateDirectory(workingDirectory);
        }

        #endregion
    }
}